<template>
    <div class="logincontainer">
        <div class="login">
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-row>
                        <el-col :span="16">
                            <el-input v-model="ruleForm.code"></el-input>
                        </el-col>
                        <el-col :span="8">
                            <el-image
                                @click="handleCaptchaImage"
                                :src="url"
                                fit="fill"
                            ></el-image>
                        </el-col>
                    </el-row>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')"
                        >登录</el-button
                    >
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie'
import {getCaptchaImage,login} from "@/api/login"
  export default {
    data() {
      return {
        ruleForm: {
          username: '',  //用户名
          password: '',
          code: '',
          uuid:""
        },
        url: '',  //验证码图片地址
        rules: {
          username: [
            { required: true, message1: '请输入用户名', trigger: 'blur' }
            
          ],
          password: [
            { required: true, message: '输入密码', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '输入验证码', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async(valid) => {
          if (valid) {
            alert('submit!');
          const result =await login(this.ruleForm)
          if(result.data.code === 200){
            // 把token存储到cookie

            Cookies.set('ruoyitoken',result.data.token);
            this.$router.push("/")
          }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleCaptchaImage(){
        getCaptchaImage().then(res=>{
          this.url ="data:image/png;base64,"+ res.data.img
          this.ruleForm.uuid = res.data.uuid;
        })
      }
    },
    created(){
      this.handleCaptchaImage()  //加载验证码
    }
  }
</script>


<style scoped>
/* Your styles here */
.logincontainer {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    background: url('https://img202.yun300.cn/img/s05bg.jpg?tenantId=238938&viewType=1&k=1632806193000')
        no-repeat center center;
}
.login {
    width: 500px;
    height: 300px;
    background-color: rgba(255, 255, 255, 0.5);
}
</style>